package com.aviation.air1.controller;


import com.aviation.air1.entity.*;
import com.aviation.air1.service.IAviationService1;
import com.aviation.types.Response;
import com.aviation.types.ResponseCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jdk.nashorn.internal.ir.ReturnNode;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.transform.Result;
import java.util.List;
import java.util.Stack;

/**
 * @Author: dsf
 * @Description:
 * @DateTime: 2025/4/22 16:58
 **/
@RestController
@RequestMapping("/aviation")
@Slf4j
public class AviationController {

    @Resource
    private IAviationService1 aviationService;

    /**
     * 接收数据
     *
     * @param aviationEntity
     * @return
     */
    @PostMapping("/receive")
    public Response receiveData(@RequestBody AviationEntity aviationEntity) {
        try {
            if(aviationEntity.getId() == null){
                aviationEntity.setId(IdWorker.getId());
            }
            log.info("id为：{}",aviationEntity.getId());
            aviationService.save(aviationEntity);
            return Response
                    .builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } catch (Exception e) {
            return Response.<List<AviationEntity>>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    /**
     * 与或非高级检索
     *
     * @param searchConditionEntities
     * @return
     */
    @PostMapping("/retrieval")
    public Response<Page<AviationEntity>> retrievalData(@RequestBody List<SearchConditionEntity> searchConditionEntities) {
        try {

            Page<AviationEntity> pageResult = aviationService.retrieval(searchConditionEntities);
            return Response.<Page<AviationEntity>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(pageResult)
                    .build();
        } catch (Exception e) {
            return Response.<Page<AviationEntity>>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    /**
     * 分页查询
     *
     * @param name
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/page")
    public Response<Page<AviationEntity>> page(@RequestParam(defaultValue = "") String name,
                                               @RequestParam Integer pageNum,
                                               @RequestParam Integer pageSize) {
        try {
            QueryWrapper<AviationEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("id");
            if (!"".equals(name)) {
                queryWrapper.like("network_state", name);
            }
            Page<AviationEntity> page = aviationService.page(new Page<>(pageNum, pageSize), queryWrapper);
            return Response.<Page<AviationEntity>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(page)
                    .build();
        } catch (Exception e) {
            return Response.<Page<AviationEntity>>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    /**
     * 删除接口
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Response delete(@PathVariable Long id) {
        try {

            boolean isSuccess = aviationService.removeById(id);
            if (isSuccess) {
                return Response.builder()
                        .code(ResponseCode.SUCCESS.getCode())
                        .info(ResponseCode.SUCCESS.getInfo())
                        .build();
            } else {
                return Response.builder()
                        .code(ResponseCode.UN_ERROR.getCode())
                        .info(ResponseCode.UN_ERROR.getInfo())
                        .build();
            }
        } catch (Exception e) {
            return Response.builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @GetMapping("/count")
    public Response<CountEntity> count(){
        try {
            CountEntity countEntity = aviationService.countAviation();
            return Response.<CountEntity>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(countEntity)
                    .build();
        }catch (Exception e){
            return Response.<CountEntity>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    /**
     * 统计机场飞机数
     * @return
     */
    @GetMapping("/count_airport")
    public Response<List<AirportAircraftCountVO>> countAirport(){
        try {
            List<AirportAircraftCountVO> aircraftCountByAirport = aviationService.getAircraftCountByAirport();
            return Response.<List<AirportAircraftCountVO>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(aircraftCountByAirport)
                    .build();
        }catch (Exception e){
            e.printStackTrace();
            return Response.<List<AirportAircraftCountVO>>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    /**
     * 保存接口
     * @param aviationEntity
     * @return
     */
    @PostMapping("/save")
    public Response save(@RequestBody AviationEntity aviationEntity){
        try {
            boolean isSucess = aviationService.saveOrUpdate(aviationEntity);
            if (isSucess){
                return Response.<List<AirportAircraftCountVO>>builder()
                        .code(ResponseCode.SUCCESS.getCode())
                        .info(ResponseCode.SUCCESS.getInfo())
                        .build();
            }else{
                return Response.<List<AirportAircraftCountVO>>builder()
                        .code(ResponseCode.UN_ERROR.getCode())
                        .info(ResponseCode.UN_ERROR.getInfo())
                        .build();
            }
        }catch (Exception e){
            e.printStackTrace();
            return Response.<List<AirportAircraftCountVO>>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

}
